<!doctype html>
<html>
<head>
    <include file="Include:meta"/>
    <title>添加供应商-目的地运营集团数据管理平台</title>
    <include file="Include:style"/>
    <link rel="stylesheet" type="text/css" href="http://xoxco.com/examples/jquery.tagsinput.css" />
    <!--<script type="text/javascript" src="http://xoxco.com/examples/jquery.min.js"></script>-->
    <script type="text/javascript">

        function onAddTag(tag) {
            alert("Added a tag: " + tag);
        }
        function onRemoveTag(tag) {
            alert("Removed a tag: " + tag);
        }

        function onChangeTag(input,tag) {
            alert("Changed a tag: " + tag);
        }

        $(function() {

            $('#tags_1').tagsInput({width: '400px'});
        });
    </script>
</head>
<body>
<include file="Include:header"/>
<include file="Include:left"/>
<div id="main">
    <div class="crumbs">
        <div class="crumbs-text">供应商库 > 添加供应商</div>
    </div>
    <div id="content">
        <form action="__APP__/ProductSupplierManage/add" method="post" enctype="multipart/form-data">
            <div class="form-main">
                <div class="form-title">添加供应商</div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>产品类型</div>
                    <div class="input-item">
                        <select name="type" class="form-control select-add" autocomplete="off">
                            <option value="">请选择</option>
                            <foreach name="result_product" item="vo">
                                <option value="{$vo['type']}">{$vo['type']}</option>
                            </foreach>
                        </select>
                        <input type="text" name="new_type" class="form-control" autocomplete="off"
                               placeholder="新类型在此输入框填写">
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>供应商名称</div>
                    <div class="input-item">
                        <input type="text" name="name" class="form-control" autocomplete="off">
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>联系人</div>
                    <div class="input-item">
                        <input type="text" name="linkman" class="form-control" autocomplete="off">
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>联系人电话</div>
                    <div class="input-item">
                        <input type="text" name="link_phone" class="form-control" autocomplete="off">
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">地址</div>
                    <div class="input-item">
                        <select name="province_id" class="form-control w100" autocomplete="off">
                            <option value='-10'>省</option>
                            <foreach name="province_list" item="vo">
                                <option value="{$vo['id']}">{$vo['name']}</option>
                            </foreach>
                        </select>
                        <select name="city_id" class="form-control w100" autocomplete="off">
                            <option value='-10'>市</option>
                        </select>
                        <input type="text" name="address" class="form-control w380" placeholder="详细地址（可不填）" autocomplete="off">
                        <span class="error-item"></span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>标签</div>
                    <div class="input-item">
                        <input id="tags_1" type="text"  name="label" class="tags" value="">
                        <!--<input type="text" name="label" class="form-control" autocomplete="off">-->
                        <span class="error-item">必填项</span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label"><span class="asterisk">*</span>区分</div>
                    <div class="input-item">
                        <span class="mr10 checkbox-diff">
                        <label class="radio-inline" for="diff1">
                            <input type="radio" name="diff" id="diff1" value="1">内部产品
                        </label>
                        <label class="radio-inline" for="diff2">
                            <input type="radio" name="diff" id="diff2" value="2">外部产品
                        </label>
                        <span class="error-item">必填项</span>
                        </span>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">公司网址</div>
                    <div class="input-item">
                        <input type="text" name="url" class="form-control" autocomplete="off">
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">公司简介</div>
                    <div class="input-item">
                        <textarea name="intro" class="form-control w400 h200" autocomplete="off"></textarea>
                    </div>
                </div>
                <div class="form-item">
                    <div class="input-label">附件</div>
                    <div class="input-item">
                        <div class="input-group w250 left">
                            <input type="text" name="file_name" class="form-control file-name" placeholder="请上传附件"
                                   autocomplete="off">
                            <span class="input-group-btn">
				        		<button class="btn file-submit" type="button">上传</button>
					    	</span>
                        </div>
                        <button class="btn file-delete ml5 hide" type="button">删除</button>
                        <input type="hidden" name="file_url" class="file-url" autocomplete="off">
                        <span class="error-item">请上传文件</span>
                        <span class="error-item">文件名必填</span>
                    </div>
                </div>
            </div>
            <div class="form-btn-item">
                <button class="btn submit" type="submit">提交</button>
                <button class="btn btn-default goback" type="button">返回</button>
            </div>
        </form>
    </div>
</div>
<include file="Include:javascript"/>
<script type="text/javascript" src="__PUBLIC__/js/tagsinput.js"></script>
<script type="text/javascript">
    $(function () {
        /*省市联动*/
        $("[name='province_id']").change(function(){
            var id = $(this).val();
            $.ajax({
                type:"POST",
                dataType:'json',
                data:{'region':id,'type':1,'bool':1},
                url:"__APP__/Communal/get_province_city",
                success:function(data){
                    var option = "";
                    if(data){
                        option += "<option value='-10'>请选择</option>";
                        if(data){
                            for(var i=0;i<data.length;i++){
                                option += "<option value="+data[i].id+">"+data[i].name+"</option>";
                            }
                        }
                    }else{
                        option += "<option value='-10'>市</option>";
                    }
                    $("[name='city_id']").html(option).select2();
                }
            });
        });
    })
    $(function () {
        KindEditor.ready(function (K) {
            //上传附件
            var editorFile = K.editor();
            $('.file-submit').on('click', function () {
                var parents = $(this).parents(".input-group");
                editorFile.loadPlugin('insertfile', function () {
                    document.cookie = "file_name=supplier";
                    document.cookie = "file_type=ppt,pptx,pdf,doc,docx,jpg,jpeg,png,xls,xlsx,txt,rar,zip";
                    editorFile.plugin.fileDialog({
                        clickFn: function (url, title) {
                            parents.nextAll(".error-item").hide();
                            parents.find(".file-name").val(title).prop("readonly", false);
                            parents.nextAll(".file-url").val(url);
                            parents.nextAll(".file-delete").show();
                            editorFile.hideDialog();
                        }
                    });
                });
            });
        });

        //删除附件
        $(".file-delete").click(function () {
            $(this).hide();
            $(this).nextAll(".error-item").hide();
            $(this).nextAll(".file-url").val("");
            $(this).prevAll(".input-group").find(".file-name").val("").prop("readonly", true);
        });

        //标签选择事件
        $("select.select-add").on('change', function () {
            var val = $(this).val(),
                text = $(this).nextAll(".select-add-text");
            if (val == -10) {
                text.show();
            } else {
                text.hide().val("").nextAll(".error-item").hide();
            }
        });

        //添加类型多选框
        $('.label-add-btn').on('click', function () {
            var input = $(this).prevAll(".label-add-text").eq(0),
                item = input.prev(".checkbox-item"),
                val = input.val(),
                tag = true;
            input.nextAll(".error-item").eq(1).hide();
            if (val == "" || /^\s+$/.test(val)) {
                input.nextAll(".error-item").eq(0).show();
                tag = false;
            }
            if (val == "全部") {
                input.nextAll(".error-item").eq(2).show();
                tag = false;
            } else {
                item.find(".checkbox-inline").each(function () {
                    if ($(this).text() == val) {
                        input.nextAll(".error-item").eq(2).show();
                        tag = false;
                        return false;
                    }
                });
            }

            if (tag) {
                var first_id = $(this).attr("first_id"),
                    label = item.find(".checkbox-inline:last"),
                    name = label.find(":checkbox").attr("name");
                $.ajax({
                    type: "POST",
                    async: false,
                    dataType: "json",
                    data: {"type_name": val, "first_id": first_id},
                    url: "__APP__/Index/label_add_ajax",
                    success: function (data) {
                        if (data.id) {
                            input.val("");
                            item.find(".error-item").hide();
                            var html = "<label class='checkbox-inline'><input type='radio' name=" + name + " value=" + data.id + " checked autocomplete='off'>" + data.name + "</label>";
                            label.after(html);
                        }
                    }
                });
            }
        });
        $(function(){
            //变标签
            $('#tags_1').tagsInput({width:'auto'});
        });
        //表单验证
        $(".submit").click(function () {
            var tag = false;

            $("[name='name']").each(function () {
                var val = $(this).val();
                if (val == "" || /^\s+$/.test(val)) {
                    $(this).next(".error-item").show();
                    tag = true;
                }
            });
            $("[name='linkman']").each(function () {
                var val = $(this).val();
                if (val == "" || /^\s+$/.test(val)) {
                    $(this).next(".error-item").show();
                    tag = true;
                }
            });
            $("[name='link_phone']").each(function () {
                var val = $(this).val();
                if (val == "" || /^\s+$/.test(val)) {
                    $(this).next(".error-item").show();
                    tag = true;
                }
            });
            $("[name='label']").each(function () {
                var val = $(this).val();
                if (val == "" || /^\s+$/.test(val)) {
                    $(this).next(".error-item").show();
                    tag = true;
                }
            });
            $(".checkbox-diff").each(function () {
                if ($(this).find(":radio:checked").length == 0) {
                    $(this).find(".error-item").show();
                    tag = true;
                }
            });
            $("select.select-add").each(function () {
                var $this = $(this),
                    val = $this.val(),
                    text = $this.nextAll(".select-add-text").val();
                if (val == -10 && (text == "" || /^\s+$/.test(text))) {
                    $this.nextAll(".error-item").eq(0).show();
                    tag = true;
                }
                if (text == "全部") {
                    $this.nextAll(".error-item").eq(1).show()
                    tag = true;
                } else {
                    $this.find("option").each(function () {
                        if ($(this).text() == text) {
                            $this.nextAll(".error-item").eq(1).show()
                            tag = true;
                            return false;
                        }
                    });
                }
            });
            $(".checkbox-item").each(function () {
                if ($(this).find(":radio:checked").length == 0) {
                    $(this).find(".error-item").show();
                    tag = true;
                }
            });
            $(".label-add-text").each(function () {
                $val = $(this).val();
                if ($val != "") {
                    $(this).nextAll(".error-item").eq(1).show();
                    tag = true;
                }
            });
            if (tag) {
                return false;
            }
        });

        //取消错误提示
        $("[name='name'],.select-add-text,.label-add-text").focus(function () {
            $(this).nextAll(".error-item").hide();
        });
        $("[name='linkman'],.select-add-text,.label-add-text").focus(function () {
            $(this).nextAll(".error-item").hide();
        });
        $("[name='link_phone'],.select-add-text,.label-add-text").focus(function () {
            $(this).nextAll(".error-item").hide();
        });
        $("[name='label'],.select-add-text,.label-add-text").focus(function () {
            $(this).nextAll(".error-item").hide();
        });
        $(".checkbox-item").on('click', ":checkbox", function () {
            $(this).parents(".checkbox-item").find(".error-item").hide();
        });
        $(".checkbox-diff").on('click', ":checkbox", function () {
            $(this).parents(".checkbox-diff").find(".error-item").hide();
        });
        $(".file-name").on("focus", function () {
            $(this).parents(".input-group").nextAll(".error-item").hide();
        });
    });
</script>
</body>
</html>